package com.kiln.juxingqs.ui

import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.AvailableAssetsFragmentPageAdapter
import com.kiln.juxingqs.databinding.ActivityFixedassetsBinding
import com.kiln.juxingqs.dialog.DateTimeDialog
import com.kiln.juxingqs.ui.vm.FixedAssetsViewModel
import java.util.*


class FixedAssetsActivity :
    BaseMiddleActivity<FixedAssetsViewModel, ActivityFixedassetsBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_fixedassets
    }

    val adapter: AvailableAssetsFragmentPageAdapter by lazy {
        AvailableAssetsFragmentPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): FixedAssetsViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel

    }
    fun searchData() {
        viewModel.pageOne.value = 0
        viewModel.holdOne()
    }

    fun current() {
        val calendar = Calendar.getInstance()
        val y = calendar.get(Calendar.YEAR)
        val m = calendar.get(Calendar.MONTH) + 1
        val d = calendar.get(Calendar.DAY_OF_MONTH)

        viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"

        var mm = m - 2
        var yy = y
        if (mm <= 0) {
            mm = 12 - mm
            yy--
        }
        viewModel.startDate.value =
            "${yy}-${if (mm < 10) "0${mm}" else mm}-01"

        searchData()
    }
    fun selectDate(index: Int) {
        DateTimeDialog(this@FixedAssetsActivity).apply {
            ok = { y, m, d ->
                if (index == 0) {
                    viewModel.startDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
//                    toast("${y}-${if (m < 10) "0${m}" else m}-${d}")
                } else {
                    viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
                }
            }
        }.show()
    }
}